package com.lyxs916.testlibrary.recyclertest;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.lyxs916.appbaselibrary.core.activity.BaseRefreshActivity;
import com.lyxs916.testlibrary.R;
import com.lyxs916.adapter.ViewHolder;
import com.lyxs916.adapter.recyclerview.CommonAdapter;
import com.lyxs916.xrecycleview.SuperRecyclerView;
import com.lyxs916.utilslibrary.ui.divider.HorizontalDividerItemDecoration;
import com.lyxs916.utilslibrary.view.SupperToolBar;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明:自动加载RecyclerView 请求测试，这是继承与BaseListActivity实现
 * 作者： 杨阳; 创建于：  2017-06-14  10:02
 */
public class RefreshTestActivity extends BaseRefreshActivity<IListTestParentView.IRefreshTestView, RefreshTestPresenter> implements IListTestParentView.IRefreshTestView {
    SupperToolBar toolbar;
    SuperRecyclerView switchRootId;
    ArrayList<ListTestBean> mDatas = new ArrayList<>();
    CommonAdapter adapter;
    ViewGroup miewGroup;

    public static void startUi(Context context) {
        Intent intent = new Intent(context, RefreshTestActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setContentView() {
        return R.layout.test_activity_list;
    }

    @Override
    public void parseIntent(Intent intent) {
    }

    @Override
    public void initView() {
        toolbar = (SupperToolBar) findViewById(R.id.toolbar);
        miewGroup = (ViewGroup) findViewById(R.id.rootView);
        switchRootId = (SuperRecyclerView) findViewById(R.id.superRecyclerView);

        toolbar.setTitle("只刷新请求测试");
        toolbar.setBack(this);

        switchRootId.getRecyclerView().setLayoutManager(new LinearLayoutManager(this));
        switchRootId.getRecyclerView().addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).sizeResId(R.dimen.dp_5).colorResId(R.color.gray_ee).build());
        switchRootId.setAdapter(adapter = new CommonAdapter<ListTestBean>(this, R.layout.test_item_test, mDatas) {
            @Override
            public void convert(ViewHolder holder, ListTestBean s) {
                holder.setText(R.id.test, "第" + holder.getmPosition() + "条： " + s.getHourNumber());
            }
        });

        presenter.requestHttpData(true, miewGroup);
    }

    @Override
    public RefreshTestPresenter initPresenter() {
        return new RefreshTestPresenter();
    }

    @Override
    public void resultData(List<ListTestBean> data) {
        mDatas.addAll(data);
        switchRootId.getRecyclerView().setNoData("4567891563");
        adapter.notifyDataSetChanged();
    }

    @Override
    public void coreOnLoadingBarListener(View parent) {
        presenter.requestHttpData(true, miewGroup);
    }

    @Override
    public void onRefresh() {
        adapter.notifyDataSetChanged();
        presenter.requestHttpData(true, miewGroup);
    }

    @Override
    public void clearData() {
        mDatas.clear();
    }

    @Override
    public SwipeRefreshLayout getSwipeRefreshLayout() {
        return  ((SuperRecyclerView) findViewById(R.id.superRecyclerView)).getSwipeRefreshLayout();
    }
}
